package com.cjh.lms.controller;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.print.attribute.standard.RequestingUserName;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cjh.lms.json.JsonResult;
import com.cjh.lms.pojo.TReq;
import com.cjh.lms.pojo.business.LockCheckModel;
import com.cjh.lms.pojo.business.LockClientModel;
 
 
import com.cjh.lms.pojo.business.ReturnMsgModel;
import com.cjh.lms.service.AllocationManageService;
import com.cjh.lms.service.TestService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/json")
public class AllocationManageController {
	private static final Log logger = LogFactory.getLog(AllocationManageController.class);

	@Autowired
	AllocationManageService allocationManageService;
	@Autowired
	TestService testService;

	// 分管平台中：接受申请信息，插入DB
	@RequestMapping("/addRequest")
	@ResponseBody
	public ReturnMsgModel addRequest(@RequestBody TReq tReq) {
		ReturnMsgModel returnMsgModel = new ReturnMsgModel();
		tReq.setDate(new Date());
		tReq.setrCode(UUID.randomUUID().toString().replace("-", ""));
		tReq.setState(0);
		tReq.setDate(new Date());
		String cCode = tReq.getcCode();
		boolean selectByCcode = allocationManageService.selectByCcode(cCode);
		if (selectByCcode == true) {
			allocationManageService.addRequest(tReq);
			returnMsgModel.setCode(200);// 200 表示申请锁的请求提交成功
			return returnMsgModel;

		} else {
			returnMsgModel.setCode(201);// 201表示申请锁的请求提交失败
			return returnMsgModel;
		}
	}

	// 总平台：点击"申请审核"按钮后,分页显示
	// http://localhost:8080/LockManagerSystem/json/showCheckList?pageSize=2&pageCurrent=1
	@RequestMapping("/showCheckList")
	@ResponseBody
	public JsonResult showCheckList(@RequestParam int pageSize, @RequestParam int pageCurrent) {

		JsonResult jsonResult = new JsonResult();
		PageHelper.startPage(pageCurrent, pageSize);
		List<LockCheckModel> showCheckList = allocationManageService.showCheckList();
		PageInfo<LockCheckModel> pageInfo = new PageInfo<>(showCheckList);
		jsonResult.setList(pageInfo.getList()); // 当前页的所有记录
		jsonResult.setTotalRow((int) pageInfo.getTotal());// 总共有多少条记录
		jsonResult.setPageSize(pageInfo.getPageSize());
		jsonResult.setPageCurrent(pageInfo.getPageNum());
		return jsonResult;
	}

	// "申请审核"按钮提交后，开始审核
	// http://localhost:8080/LockManagerSystem/json/checkRequest?rCode=1
	@RequestMapping("/checkRequest")
	@ResponseBody
	public ReturnMsgModel checkRequest(@RequestParam String rCode) {
		ReturnMsgModel returnMsgModel = new ReturnMsgModel();
		TReq tReq = allocationManageService.selectByRCode(rCode).get(0);
		int requestCount = tReq.getCount();
		long countLock = allocationManageService.countLock();
		logger.info("根据库存和申请数量开始判断");
		if (requestCount > countLock) {
			returnMsgModel.setCode(211); // 申请数量超过库存，审核未通过;
			tReq.setState(2);// 2表示审核未通过
			allocationManageService.updateState(tReq);// 审核未通过，根据example更新state
		} else {
			returnMsgModel.setCode(210);// 库存大于申请数量，审核通过;
			tReq.setState(1);// 1表示审核通过
			allocationManageService.updateLockInfo(requestCount, tReq.getcCode());
			allocationManageService.updateState(tReq);

		}
		return returnMsgModel;
	}

	// 获取未通过审核的申请记录
	// http://localhost:8080/LockManagerSystem/json/getRequestList2?pageSize=2&pageCurrent=1
	@RequestMapping("/getRequestList2")
	@ResponseBody
	public JsonResult getRequestList2(@RequestParam int pageSize, @RequestParam int pageCurrent) {
	
		JsonResult jsonResult = new JsonResult();
		PageHelper.startPage(pageCurrent, pageSize);
		List<TReq> selectUnpassedRequest = allocationManageService.selectUnpassedRequest();
		PageInfo<TReq> pageInfo = new PageInfo<>(selectUnpassedRequest);

		jsonResult.setList(pageInfo.getList()); // 当前页的所有记录
		jsonResult.setTotalRow((int) pageInfo.getTotal());// 总共有多少条记录
		jsonResult.setPageSize(pageInfo.getPageSize());
		jsonResult.setPageCurrent(pageInfo.getPageNum());
		return jsonResult;
	}

	// 获取通过审核的申请记录
	// http://localhost:8080/LockManagerSystem/json/getRequestList1?pageSize=2&pageCurrent=1
	@RequestMapping("/getRequestList1")
	@ResponseBody
	public JsonResult getRequestList1(@RequestParam int pageSize, @RequestParam int pageCurrent) {
		JsonResult jsonResult = new JsonResult();
		PageHelper.startPage(pageCurrent, pageSize);
		List<TReq> selectPassedRequest = allocationManageService.selectPassedRequest();
		PageInfo<TReq> pageInfo = new PageInfo<>(selectPassedRequest);

		jsonResult.setList(pageInfo.getList()); // 当前页的所有记录
		jsonResult.setTotalRow((int) pageInfo.getTotal());// 总共有多少条记录
		jsonResult.setPageSize(pageInfo.getPageSize());
		jsonResult.setPageCurrent(pageInfo.getPageNum());
		return jsonResult;
	}

	// 获取待审核的申请记录
	// http://localhost:8080/LockManagerSystem/json/getRequestList0?pageSize=2&pageCurrent=1
	@RequestMapping("/getRequestList0")
	@ResponseBody
	public JsonResult getRequestList0(@RequestParam int pageSize, @RequestParam int pageCurrent) {
		JsonResult jsonResult = new JsonResult();
		PageHelper.startPage(pageCurrent, pageSize);
		List<TReq> selectToBeChecked = allocationManageService.selectToBeChecked();
		PageInfo<TReq> pageInfo = new PageInfo<>(selectToBeChecked);

		jsonResult.setList(pageInfo.getList()); // 当前页的所有记录
		jsonResult.setTotalRow((int) pageInfo.getTotal());// 总共有多少条记录
		jsonResult.setPageSize(pageInfo.getPageSize());
		jsonResult.setPageCurrent(pageInfo.getPageNum());
		return jsonResult;
	}

	// 获取申请单位信息
	// http://localhost:8080/LockManagerSystem/json/getClientInfo?pageSize=2&pageCurrent=1
	@RequestMapping("/getClientInfo")
	@ResponseBody
	public JsonResult getClientInfo(@RequestParam int pageSize, @RequestParam int pageCurrent) {
		JsonResult jsonResult = new JsonResult();
		PageHelper.startPage(pageCurrent, pageSize);
		List<LockClientModel> clientModels = allocationManageService.getclientInfo();
		PageInfo<LockClientModel> pageInfo = new PageInfo<>(clientModels);

		jsonResult.setList(pageInfo.getList()); // 当前页的所有记录
		jsonResult.setTotalRow((int) pageInfo.getTotal());// 总共有多少条记录
		jsonResult.setPageSize(pageInfo.getPageSize());
		jsonResult.setPageCurrent(pageInfo.getPageNum());
		return jsonResult;
	}
	

}
